<?php
/**
 *  玩家一键领取邮件附件
 */

class OneKeyReceive extends APIBase {
	public $tag = "OneKeyReceive";
	public $isLogin = true;
	
	public function before() {
		$this->initMysql();
		$this->inithttp();
		$this->initCacheRedis();
		return true;
	}
			
	public function logic() {
	    $uid = $this->uid;
		$keys = $this->cache_redis->keys('hemailbyuser:'.$uid.':*');
		$data = array();
		if(count($keys)>0){
			for($i=1;$i<=count($keys);$i++){
				//获取玩家的邮件
				$emailinfo = $this->cache_redis->hMget('hemailbyuser:'.$uid.':'.$i,array('mid','is_receive'));
				//判断出没领取的邮件
				if($emailinfo['is_receive']==0 && $emailinfo['is_read']!=9){
				    $hmg = $this->cache_redis->hMget('hmg:'.$emailinfo['mid'],array('rid'));
				    //判断邮件是否有附件可领取
				    if($hmg['rid']!=''){
					    $ridArr = explode(',',$hmg['rid']);
					    $reward = array();					
					    foreach($ridArr as $key=>$val){
						    $reward = $this->cache_redis->hMget('hreward:'.$val,array('propid','reward_quantity'));
						    $isExistsProp = $this->cache_redis->exists('hbackpack:'.$uid.':'.(int)$reward['propid']);
						    //若背包存在相同道具,则数量叠加
						    if($isExistsProp){
							    //房卡
							    if((int)$reward['propid']==1){
								    $row = $this->cache_redis->hMget('hbackpack:'.$uid.':'.(int)$reward['propid'],array('count'));
								    $count = (int)$reward['reward_quantity']+(int)$row['count'];
								    $this->cache_redis->hMset('hbackpack:'.$uid.':'.(int)$reward['propid'],array('count'=>$count,'status'=>1));
							        $user = $this->data_redis->hMGet('hu:'.$uid,array('round_card_num'));
							        $round_card_num = (int)$user['round_card_num']+(int)$reward['reward_quantity'];
					                $this->data_redis->hMset('hu:'.$uid,array('round_card_num'=>$round_card_num));
							    }else{//其他道具
									
								}
							
						    }else{//若没有相同道具,则新添
							    $data = array('uid'=>$uid,'propid'=>(int)$reward['propid'],'count'=>$reward['reward_quantity'],'createtime'=>time(),'status'=>1);
					            $this->cache_redis->hMset('hbackpack:'.$uid.':'.(int)$reward['propid'],$data);
						    }
					    }
						//改变邮件状态
						$this->cache_redis->hMset('hemailbyuser:'.$uid.':'.$i,array('is_read'=>1,'is_receive'=>1,'update_date'=>time()));
				    }
				}	
			}
			$this->returnData('success');
		}elseif(count($keys)<=0){
			$this->returnError(301,'玩家尚未有附件可领取');
		}
	}

	public function after() {
		$this->deinitDataRedis();
		$this->deinitMysql();
	}
}

?>
